<?php
/**
 * Created by PhpStorm.
 * User: Merdan
 * Date: 2017/3/27
 * Time: 1:23
 */

namespace App\Services;
use App\Exceptions\NotExistException;
use App\Models\Canteen as CanteenModel;

class Canteen extends BaseService
{
    /**
     * 餐厅列表
     *
     * @param $limit
     * @return mixed
     */
    public function getList($limit)
    {
        return CanteenModel::with('user')->paginate($limit);
    }

    /**
     * 获取单个餐厅数据
     *
     * @param $id
     * @return mixed
     * @throws NotExistException
     */
    public function show($id)
    {
        $canteen = CanteenModel::with('user')->find($id);
        if(!$canteen) throw new NotExistException('你查看的餐厅不存在');
        return $canteen;
    }

    /**
     * 创建新的餐厅
     *
     * @param $data
     * @return CanteenModel
     */
    public function create($data)
    {
        return CanteenModel::create($data);
    }

    /**
     * 更新餐厅
     *
     * @param $id
     * @param $data
     * @return CanteenModel
     * @throws NotExistException
     */
    public function update($id, $data)
    {
        $data = array_filter($data, function ($item){
            return $item != null;
        });
        $canteen = CanteenModel::with('user')->find($id);
        if(!$canteen) throw new NotExistException('你查看的餐厅不存在');
        $canteen->update($data);
        return $canteen;
    }
}